from django.db import models

# Create your models here.


class CommonModel(models.Model):
    '''自定义模板基类'''
    created_at = models.DateTimeField('添加记录时间', auto_now_add=True)
    updated_at = models.DateTimeField('最后修改时间', auto_now=True)

    class Meta:
        '''抽象类，这个类不会生成对应的数据库'''
        abstract=True


class User(CommonModel):
    '''用户基本信息'''
    # name = models.CharField('姓名', max_length=128)
    # sex = models.CharField('性别', max_length=1, choices=(
    #     ('1','男'),
    #     ('0', '女')
    # ), default='1')
    # age = models.PositiveIntegerField('年龄', default=0)
    USER_STATUS=(
        (1, '正常'),
        (0, '删除'),
    )
    username = models.CharField('用户名', max_length=128, unique=True)
    password = models.CharField('密码', max_length=256)
    nickname = models.CharField('用户昵称', max_length=256, null=True, blank=True)
    avatar = models.ImageField('用户头像', upload_to='avatar', null=True, blank=True)
    status = models.SmallIntegerField('用户状态', default=1, choices=USER_STATUS)
    is_super = models.BooleanField('是否为超级用户', default=False)

    class Meta:
        db_table='accounts_user'

    def __str__(self):
        return 'User：{}'.format(self.username)



class UserProfile(CommonModel):
    '''用户详细信息'''
    # zhangsan=User()
    # zhangsan.Profile
    SEX_CHOICES = (
        ('1','男'),
        ('2', '女'),
        ('0', '未知'),
    )
    user = models.OneToOneField(User, verbose_name='关联用户', on_delete=models.CASCADE,
                                related_name='profile')
    username = models.CharField('用户名', max_length=128, unique=True)
    real_name = models.CharField('真实姓名', max_length=128, null=True, blank=True)
    sex = models.CharField('性别', max_length=1, choices=SEX_CHOICES, default='0')
    maxim = models.CharField('用户格言', max_length=128, null=True, blank=True)
    address = models.CharField('用户地址', max_length=128, null=True, blank=True)

    class Meta:
        db_table='accounts_user_profile'

class LoginHistory(models.Model):
    '''用户登录历史'''

    user = models.ForeignKey(User, verbose_name='关联用户', on_delete=models.CASCADE,
                                related_name='login_history_list')
    username = models.CharField('用户名', max_length=128)
    login_type = models.CharField('账号平台', max_length=128)
    ip = models.CharField('IP地址', max_length=32, default='')
    ua = models.CharField('登录来源', max_length=128, default='')
    created_at = models.DateTimeField('登录时间', auto_now_add=True)

    class Meta:
        db_table='accounts_login_history'
        ordering = ['-created_at']